package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.ShowModel;
import com.zeng.jiudian.dao.ShowDao;
import com.zeng.jiudian.mapper.ShowMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/25 - 16:28
 */
public class ShowDaoImpl implements ShowDao
{
    private SqlSession sqlSession;

    @Override
    public int add(ShowModel showModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        showModel.setCreateTime(dateFormat.format(new Date()));
        showModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ShowMapper showMapper = sqlSession.getMapper(ShowMapper.class);
            count = showMapper.insert(showModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<ShowModel> findAll(ShowModel showModel)
    {
        List<ShowModel> list = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ShowMapper showMapper = sqlSession.getMapper(ShowMapper.class);
            list = showMapper.selectAll(showModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int getCount(ShowModel showModel)
    {
        int count = 0;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ShowMapper showMapper = sqlSession.getMapper(ShowMapper.class);
            count = showMapper.selectCount(showModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int delete(int id)
    {
        int count = 0;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ShowMapper showMapper = sqlSession.getMapper(ShowMapper.class);
            count = showMapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public ShowModel findById(int id)
    {
        ShowModel model = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ShowMapper showMapper = sqlSession.getMapper(ShowMapper.class);
            model = showMapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return model;
    }

    @Override
    public int update(ShowModel showModel)
    {
        int count = 0;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ShowMapper showMapper = sqlSession.getMapper(ShowMapper.class);
            count = showMapper.updateByPrimaryKeySelective(showModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<ShowModel> findUnion()
    {
        List<ShowModel> list = new ArrayList<>();

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ShowMapper mapper = sqlSession.getMapper(ShowMapper.class);
            list = mapper.selectUnion();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return list;
    }
}
